var token = localStorage.getItem('token'),
    idDelNum = '1',
    publishedData = Sendajax("http://119.23.46.241:8080/admin/release/selectpublished", "post", { isDel: idDelNum }, token)

//显示
setTimeout(function() {
    showData(publishedData.data, "deleted-button", "删除")
}, 100)

//切换已删除页
$(".deleted").on('click', function() {
    let state = $(this).attr('data-state')
    if (state == 1) {
        $(this).text('管理发布').addClass('manageReleases').removeClass('deleted')
        $(".manage-releases").text('已删除')
        $("#container").html('<section class="manage-releases">已删除的文章</section>')
        $(this).attr('data-state', 0)
        idDelNum = $(this).attr('data-state')
        deletedData = Sendajax("http://119.23.46.241:8080/admin/release/selectpublished", "post", { isDel: idDelNum }, token)
        showData(deletedData.data, "recovery-button", "恢复")
    }
    if (state == 0) {
        $(this).text('已删除').addClass('deleted').removeClass('manageReleases')
        $(".manage-releases").text('管理发布')
        $("#container").html('<section class="manage-releases">管理已发布的文章</section>')
        $(this).attr('data-state', 1)
        idDelNum = $(this).attr('data-state')
        publishedData = Sendajax("http://119.23.46.241:8080/admin/release/selectpublished", "post", { isDel: idDelNum }, token)
        showData(publishedData.data, "deleted-button", "删除")
    }
})

//显示信息
function showData(data, classVal, text) {
    if (data.length <= 3) {
        $("footer").css({ "position": "absolute", "bottom": "0" })
    } else {
        $("footer").css({ "position": "" })
    }
    if (data.length == 0) {
        $(".manage-releases").after(`<section class="no-content">
        <section></section>
         <p>暂无内容</p>
         </section>`)
    } else {
        let showData = ''
        $.each(data, function(i, val) {
            if (val["titImg"]) {
                showData += `<section class="release deleted-release">
        <section class="deleted-release-information">
            <section class="deleted-release-image">
                <img src="${val['titImg']}" alt="">
            </section>
            <section class="deleted-release-content">
                <section class="deleted-release-title release-title" data-Id="${val['id']}">
                    <p>${val['title']}</p>
                </section>
                <section class="deleted-release-label">${val['label']}</section>
                <section class="deleted-release-address">${val['position']}</section>
                <section class="deleted-release-time">
                    <p>${val['time']}</p>
                </section>
            </section>
        </section>
        <section class="deleted-release-time-operation">
            <section class="deleted-release-operation">
                <span>点赞${val['like']}</span>
                <span>浏览${val['look']}</span>
                <span>转发${val['forward']}</span>
            </section>
            <section class="${classVal}">
                <p>${text}</p>
            </section>
        </section>
    </section>`
            } else {
                showData += `<section class="release deleted-release-text">
        <section class="deleted-release-text-information">
            <section class="deleted-release-text-title release-title" data-Id="${val['id']}">
                <p>${val['title']}</p>
            </section>
            <section class="deleted-release-text-label">${val['label']}</section>
            <section class="deleted-release-text-time">
                <p>${val['time']}</p>
            </section>
            <section class="deleted-release-text-operation">
                <span>点赞${val['like']}</span>
                <span>浏览${val['look']}</span>
                <span>转发${val['forward']}</span>
            </section>
        </section>
        <section class="deleted-release-text-address-operation">
            <section class="deleted-release-text-address">${val['position']}</section>
            <section class="${classVal}"><p>${text}</p></section>
        </section>
    </section>`
            }
        })
        showData += `<section class="end">
        <section></section>
        <p>到底了</p>
        </section>`
        $(".manage-releases").after(showData)
    }

    //删除已发布
    $(".deleted-button").on("click", function() {
        Sendajax("http://119.23.46.241:8080/admin/release/delorinsrelease", "post", {
            id: $(this).parents(".release").find(".release-title").attr("data-id"),
            isDel: idDelNum
        }, token)
        $(this).parents(".release").css({
            "box-shadow": "0 6px 20px 8px rgba(4, 0, 0, 0.06)",
            "position": "relative",
            "z-index": "10",
            "right": "0"
        }).animate({ "right": "-1000px" }, "swing", function() {
            $("#container").html('<section class="manage-releases">管理已发布的文章</section>')
            publishedData = Sendajax("http://119.23.46.241:8080/admin/release/selectpublished", "post", { isDel: idDelNum }, token)
            showData(publishedData.data, "deleted-button", "删除")
        });

    })

    //恢复已删除
    $(".recovery-button").on("click", function() {
        Sendajax("http://119.23.46.241:8080/admin/release/delorinsrelease", "post", {
            id: $(this).parents(".release").find(".release-title").attr("data-id"),
            isDel: idDelNum
        }, token)
        $(this).parents(".release").css({
            "box-shadow": "0 6px 20px 8px rgba(4, 0, 0, 0.06)",
            "position": "relative",
            "z-index": "10",
            "right": "0"
        }).animate({ "right": "-1000px" }, "swing", function() {
            $("#container").html('<section class="manage-releases">已删除的文章</section>')
            deletedData = Sendajax("http://119.23.46.241:8080/admin/release/selectpublished", "post", { isDel: idDelNum }, token)
            showData(deletedData.data, "recovery-button", "恢复")
        });
    })
}

//搜索
$(".search-box input").keydown(function(e) {
    let seeData = ''
    if (e.keyCode == 13) {
        seeData = Sendajax("http://119.23.46.241:8080/admin/release/lookforrelease", "post", {
            input: $(this).val(),
            isDel: idDelNum
        }, token)
        $(".release").remove()
        if (idDelNum == 1) {
            $(".search-box input").blur()
            showData(seeData.data, "deleted-button", "删除")
        }
        if (idDelNum == 0) {
            $(".search-box input").blur()
            showData(seeData.data, "recovery-button", "恢复")
        }
    }
})